package com.pbase.tools.bingingadapter;

import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.ClickUtils;
import com.bumptech.glide.Glide;
import com.pbase.tools.Logger;

import androidx.databinding.BindingAdapter;
import androidx.lifecycle.MutableLiveData;

/**
 * User: psn
 * Date: 2021/7/9
 * 常用BindingAdapter存放
 */
public class CommonBindingAdapter {
    /**
     * 去除重复点击 700ms
     *
     * @param view
     * @param clickListener
     */
    @BindingAdapter(value = {"onClickSingleDebouncing"}, requireAll = false)
    public static void onClickSingleDebouncing(View view, View.OnClickListener clickListener) {
        ClickUtils.applySingleDebouncing(view, clickListener);
    }


    /**
     * 输入框显示隐藏密码
     *
     * @param view
     * @param password
     */
    @BindingAdapter(value = {"isPassWord"}, requireAll = false)
    public static void isPassWord(EditText view, boolean password) {
        Logger.d("isPassWord", "isPassWord " + password);
        if (password) {
            view.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else {
            view.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
    }


    /**
     * 设置关标到输入的最尾部
     */
    @BindingAdapter(value = {"setSelectionToLast"}, requireAll = false)
    public static void setSelectionToLast(EditText view, String str) {
        view.setSelection(view.getText().length());
    }

    /**
     * 获取焦点后将光标设置到输入尾部
     */
    @BindingAdapter(value = {"setSelectionToLastOnFocusChange"}, requireAll = false)
    public static void setSelectionToLastOnFocusChange(EditText view, boolean focus) {
        view.setCursorVisible(false);
        view.setSelection(view.getText().length());
        view.setOnClickListener(v -> {
            view.setSelection(view.getText().length());
            view.setCursorVisible(true);
        });


    }

    /**
     * 监听获取焦点状态
     *
     * @param view
     * @param mutableLiveData
     */
    @BindingAdapter(value = {"setOnFocusChangeListener"}, requireAll = false)
    public static void setOnFocusChangeListener(EditText view,
                                                MutableLiveData<Boolean> mutableLiveData) {
        view.setOnFocusChangeListener((v, hasFocus) -> {
            mutableLiveData.setValue(hasFocus);
        });
    }

    @BindingAdapter(value = {"setImvSrc", "errorDrawable", "placeDrawable"}, requireAll = false)
    public static void setImvSrc(ImageView view, String url, Drawable errorDrawable,
                                 Drawable placeDrawable) {
        Glide.with(view.getContext())
                .load(url)
                .error(errorDrawable)
                .placeholder(placeDrawable)
                .into(view);

    }

    @BindingAdapter(value = {"setImvSrc", "errorDrawable", "placeDrawable"}, requireAll = false)
    public static void setImvSrc(ImageView view, Drawable url, Drawable errorDrawable,
                                 Drawable placeDrawable) {
        Glide.with(view.getContext())
                .load(url)
                .error(errorDrawable)
                .placeholder(placeDrawable)
                .into(view);

    }

    @BindingAdapter(value = {"setImvSrc", "errorDrawable", "placeDrawable"}, requireAll = false)
    public static void setImvSrc(ImageView view, int url, Drawable errorDrawable,
                                 Drawable placeDrawable) {
        Glide.with(view.getContext())
                .load(url)
                .error(errorDrawable)
                .placeholder(placeDrawable)
                .into(view);

    }
}
